{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## **方法一： 使用函数sum和range函数**"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "source": [
    "num = 10\r\n",
    "print(sum(range(0, num + 1)))"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "55\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## **方法二：使用while循环**"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "source": [
    "i = 1\r\n",
    "sum_all = 0\r\n",
    "while i <= 10:\r\n",
    "    sum_all += i\r\n",
    "    i += 1  # 等价于i=i+1\r\n",
    "print(sum_all)\r\n"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "55\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## **方法三：使用for循环**"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "def sum_start_to_end(start, end):\r\n",
    "    a = 0\r\n",
    "    for i in range(start, end + 1, 1):\r\n",
    "        a = a + i\r\n",
    "    return a\r\n",
    "\r\n",
    "\r\n",
    "print(sum_start_to_end(1, 10))\r\n"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "55\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## **方法四：使用reduce函数和lambda匿名函数**"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "reduce() 函数会对参数序列中元素进行累积。  \r\n",
    "\r\n",
    "函数将一个数据集合（链表，元组等）中的所有数据进行下列操作：  \r\n",
    "用传给 reduce 中的函数 function（有两个参数）先对集合中的第 1、2 个元素进行操作，得到的结果再与第三个数据用 function 函数运算，最后得到一个结果。  \r\n",
    "\r\n",
    "reduce() 函数语法：\r\n",
    "\r\n",
    "reduce(function, iterable[, initializer])  \r\n",
    "参数  \r\n",
    "function -- 函数，有两个参数  \r\n",
    "iterable -- 可迭代对象  \r\n",
    "initializer -- 可选，初始参数  "
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "source": [
    "from functools import reduce\r\n",
    "\r\n",
    "\r\n",
    "def sum(x, y):  # 写法一\r\n",
    "    return x + y\r\n",
    "\r\n",
    "\r\n",
    "print(reduce(sum, range(1, 11)))\r\n"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "55\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "from functools import reduce\r\n",
    "\r\n",
    "y = reduce(lambda a, b: a + b, range(1, 11))  # 写法二\r\n",
    "print(y)\r\n"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "55\n"
     ]
    }
   ],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.9.7",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.9.7 64-bit"
  },
  "interpreter": {
   "hash": "360b5204b76eb2056cbc13af1e3693270cd50165c0a9f480bb76334f67c80658"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}